using CollabApp.Application.DTOs;
using CollabApp.Application.Interfaces;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using CollabApp.Infrastructure.Data;
using CollabApp.Infrastructure.Services;
using CollabApp.Domain.Repositories;
using CollabApp.Infrastructure.Repositories;
using CollabApp.Domain.Services.Game;
using CollabApp.Application.Services.Game;

namespace CollabApp.Infrastructure;

/// <summary>
/// 扩展方法。基础服务注册。
/// </summary>
public static class ServiceCollectionExtenstion
{
    public static IServiceCollection AddInfrastructure(this IServiceCollection services, IConfiguration configuration)
    {

        // 注册JwtSettings配置
        services.Configure<JwtSettings>(configuration.GetSection("Jwt"));
        //注册JwtTokenService
        services.AddSingleton<IJwtTokenService, JwtTokenService>();
        
        //获取PostgreSql连接字符串
        var connString = configuration.GetConnectionString("pgsql");
        //注册 ApplicationDbContext，配置使用 PostgreSQL 数据库
        services.AddDbContext<ApplicationDbContext>(options =>
        {
            options.UseNpgsql(connString);
        });

        //获取redis连接字符串
        var redisConnStr = configuration.GetConnectionString("redis");
        if (string.IsNullOrWhiteSpace(redisConnStr))
        {
            throw new InvalidOperationException("Redis 连接字符串未配置!!!");
        }
        // 注册 AddSingleton, 配置使用Redis 数据库
        services.AddSingleton<IRedisService>(new RedisService(redisConnStr));

        // 注册通用仓储服务
        services.AddScoped(typeof(IRepository<>), typeof(GenericRepository<>));
        return services;
    }
}